package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart1 = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart1);
        Long userId = BaseContext.getCurrentId();

        shoppingCart1.setUserId(userId);
        List<ShoppingCart> select = shoppingCartMapper.select(shoppingCart1);

        if (select != null && select.size() > 0) {
            ShoppingCart shoppingCart = select.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumber(shoppingCart);
        } else {
            Long dishId = shoppingCart1.getDishId();
            if (dishId != null) {
                //本次购物车里添加的是菜品
                Dish dish = dishMapper.selectById(dishId);
                shoppingCart1.setName(dish.getName());
                shoppingCart1.setAmount(dish.getPrice());
                shoppingCart1.setImage(dish.getImage());

            } else {
                Long setmealId = shoppingCart1.getSetmealId();
                Setmeal setmeal = setmealMapper.selectById(setmealId);

                shoppingCart1.setName(setmeal.getName());
                shoppingCart1.setAmount(setmeal.getPrice());
                shoppingCart1.setImage(setmeal.getImage());
            }
            shoppingCart1.setNumber(1);
            shoppingCart1.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart1);

        }

    }

    @Override
    public List<ShoppingCart> select() {
        ShoppingCart shoppingCart = new ShoppingCart();
        List<ShoppingCart> select = shoppingCartMapper.select(shoppingCart);

        return select;
    }

    @Override
    public void delect(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> select = shoppingCartMapper.select(shoppingCart);

        if (select != null && select.size() > 0) {

            shoppingCart = select.get(0);
            Integer number = shoppingCart.getNumber();
            if (number == 1) {

                Long id = shoppingCart.getId();
                shoppingCartMapper.delectById(id);
            } else {
                number = number - 1;
                shoppingCart.setNumber(number);
                shoppingCartMapper.updateNumber(shoppingCart);
            }
        }


    }

    @Override
    public void clean() {


        Long currentId = BaseContext.getCurrentId();

        shoppingCartMapper.clean(currentId);
    }
}
